﻿using System;
using System.Collections.Generic;
using System.Linq;
using Pharmacy.Data;
using Pharmacy.Data.Entity;
using System.Transactions;
using Pharmacy.Data.Repositories;
using Pharmacy.Data.Core;
using Pharmacy.Ultilities.Resources;
using Pharmacy.Ultilities;


namespace Pharmacy.Business
{
    public partial class ManufacturerGroupBO : BaseBusiness
    {
        #region Constructors
        public ManufacturerGroupBO(IObjectContext objectContext) : base(objectContext) { }
        public ManufacturerGroupBO() : base() { }
        #endregion

        #region Private Methods
        public ManufacturerGroup CreateNewManufacturerGroup(string name, string note)
        {
            ManufacturerGroup newObject;
            using (TransactionScope scope = new TransactionScope())
            {
                newObject = new ManufacturerGroup();
                newObject.Name = name;
                newObject.Note = note;
                ManufacturerGroupRepository lst = new ManufacturerGroupRepository(PharmacyContext);
                lst.Add(newObject);
                PharmacyContext.SaveChanges();
                scope.Complete();
            }
            return newObject;
        }

        public bool EditManufacturerGroup(int id, string name, string note, out string messageError)
        {
            ManufacturerGroup editObject = null;
            try
            {
                ManufacturerGroupRepository lst = new ManufacturerGroupRepository(PharmacyContext);
                editObject = lst.AsQueryable().First(tmp => tmp.ID == id);
                if (editObject != null)
                {
                    messageError = editObject.IsCheckOutByAnotherUser();
                    if (messageError.Length == 0)
                    {
                        using (TransactionScope scope = new TransactionScope())
                        {
                            LockingControl.Lock(editObject);
                            editObject.Name = name;
                            editObject.Note = note;
                            PharmacyContext.SaveChanges();
                            scope.Complete();
                            LockingControl.UnLock(editObject);
                            return true;
                        }
                    }
                }
                else
                    messageError = string.Format(SR.Instance.GetString(Constant.KEY_OBJECTISDELETED), Constant.ENT_MANUFACTURERGROUP);
            }
            catch (PessimisticLockingException ex)
            {
                throw new PessimisticLockingException(string.Format(SR.Instance.GetString(Constant.MSG_LOCKBYANOTHERUSER), SR.Instance.GetString(Constant.ENT_MANUFACTURERGROUP), editObject.Name), ex.LockDataInfo);
            }
            return false;
        }

        public bool DeleteManufacturerGroup(int id, out string messageError)
        {
            ManufacturerGroup deleteObject = null;
            ManufacturerGroupRepository lst = new ManufacturerGroupRepository(PharmacyContext);
            deleteObject = lst.AsQueryable().First(tmp => tmp.ID == id);
            messageError = string.Empty;

            if (deleteObject != null)
            {
                messageError = (LockingControl as PessimisticLocking).IsCheckOutByAnotherUser(deleteObject);

                using (TransactionScope scope = new TransactionScope())
                {
                    try
                    {
                        if (messageError.Length == 0)
                        {
                            LockingControl.Lock(deleteObject);
                            lst.Delete(deleteObject);
                            PharmacyContext.SaveChanges();
                            scope.Complete();
                            LockingControl.UnLock(deleteObject);
                            return true;
                        }
                    }
                    catch (PessimisticLockingException ex)
                    {
                        throw new PessimisticLockingException(
                            string.Format(SR.Instance.GetString(Constant.MSG_LOCKBYANOTHERUSER),
                            SR.Instance.GetString(Constant.ENT_MANUFACTURERGROUP), deleteObject.Name),
                            ex.LockDataInfo);
                    }
                }
            }
            else
            {
                messageError = string.Format(SR.Instance.GetString(Constant.KEY_OBJECTISDELETED),
                    Constant.ENT_MANUFACTURERGROUP);
            }

            return false;
        }
        #endregion

        #region Public Methods

        #endregion
    }
}
